********************************************************************************
** 	TITLE:		at2017_autnes_contact_networks                                ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       10017_da_en_v1_0                                              **
**	DATE:		October 2022 					                              **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "at2017_autnes_contact_networks", replace

* Open the dataset

use "10017_da_en_v1_0.dta", clear


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen timeW2 = w2_dte
gen timeW4 = w4_dte

* Gender

gen male = sd3

recode male (2=0)
recode male (3=.)

* Age

replace age = . if age < 15

* Education

gen education = sd7

recode education (5=.)
recode education (16=.)

replace education = 1 if education == 1
replace education = 2 if education == 2
replace education = 3 if education == 3
replace education = 4 if education == 4
replace education = 5 if education == 6
replace education = 5 if education == 7
replace education = 5 if education == 8
replace education = 6 if education == 9
replace education = 7 if education == 10
replace education = 8 if education == 11
replace education = 9 if education == 12
replace education = 10 if education == 13
replace education = 11 if education == 14
replace education = 12 if education == 15

* Interest for politics (in wave 2, election attentiveness is measured rather than interest for politics)

gen interestW2 = w2_q1
gen interestW4 = w4_q1

recode interestW2 interestW4 (99=.)

replace interestW2 = 5 - interestW2
replace interestW4 = 5 - interestW4

* Party identification (national)

gen close = max(w1_q17,w4f_q24)
gen closer = max(w1_q18,w4f_q25)
gen pid = max(w1_q19,w4f_q26)
gen strength = max(w1_q20,w4f_q27)

gen pidstatus_whole = .

replace pidstatus_whole = 9999 if pid == 99

replace pidstatus_whole = 2 if close == 2 & closer == 2

replace pidstatus_whole = 3 if pid == 2 & pidstatus_whole!=2 & pidstatus_whole!=9999
replace pidstatus_whole = 3 if pid == 3 & pidstatus_whole!=2 & pidstatus_whole!=9999

replace pidstatus_whole = 1 if pid!=2 & pid!=3 & pid!=. & pidstatus_whole!=2 & pidstatus_whole!=3 & pidstatus_whole!=9999

replace pidstatus_whole = . if pidstatus_whole == 9999
 
label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID"
label values pidstatus_whole pidstatus

* Party identification scale (national)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & strength == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & strength == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & strength == 3
replace pidscale_whole = 4 if pidstatus_whole == 2
replace pidscale_whole = 5 if pidstatus_whole == 3 & strength == 3
replace pidscale_whole = 6 if pidstatus_whole == 3 & strength == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & strength == 1

label define pidscale 1 "Very strong loser PID" 2 "Fairly strong loser PID" 3 "Not very strong loser PID" 4 "No PID" 5 "Not very strong winner PID" 6 "Fairly strong winner PID" 7 "Very strong winner PID"
label values pidscale_whole pidscale

* TV news attentiveness

gen tv = 7 - w1_q38x1

recode tv (.=-99)

* Press news attentiveness

gen press = 7 - w1_q38x2

recode press (.=-99)

* Radio news attentiveness

gen radio = 7 - w1_q38x3

recode radio (.=-99)

* Internet news attentiveness

gen internet = 7 - w1_q38x4

recode internet (.=-99)

* Social media news attentiveness

gen socialmedia = 7 - w1_q38x5

recode socialmedia (.=-99)

* News attentiveness index

egen news = rowmax(tv press radio internet socialmedia)

recode news (-99=.)

* Network size (wave 2)

gen sizeW2 = w2_q43

replace sizeW2 = . if sizeW2 >= 999
replace sizeW2 = . if sizeW2 >= 999

gen sizeW2_3pts = sizeW2
replace sizeW2_3pts = 0 if sizeW2 == 1
replace sizeW2_3pts = 1 if sizeW2 == 2
replace sizeW2_3pts = 1 if sizeW2 == 3
replace sizeW2_3pts = 1 if sizeW2 == 4
replace sizeW2_3pts = 2 if sizeW2 > 4

* Network size (wave 4)

gen sizeW4 = w4_q76

replace sizeW4 = . if sizeW4 >= 999
replace sizeW4 = . if sizeW4 >= 999

gen sizeW4_3pts = sizeW4
replace sizeW4_3pts = 0 if sizeW4 == 1
replace sizeW4_3pts = 1 if sizeW4 == 2
replace sizeW4_3pts = 1 if sizeW4 == 3
replace sizeW4_3pts = 1 if sizeW4 == 4
replace sizeW4_3pts = 2 if sizeW4 > 4

* Political discussion (wave 2)

gen discussion1W2 = 4 - w2_q46
gen discussion2W2 = 4 - w2_q50

replace discussion1W2 = 0 if sizeW2 == 0
replace discussion2W2 = 0 if sizeW2 == 0 | sizeW2 < 2

gen discussionW2 = discussion1W2 + discussion2W2

gen discussion1W2_3pts = discussion1W2
replace discussion1W2_3pts = 0 if discussion1W2_3pts == 1
replace discussion1W2_3pts = 1 if discussion1W2_3pts == 2
replace discussion1W2_3pts = 2 if discussion1W2_3pts == 3

gen discussion2W2_3pts = discussion2W2
replace discussion2W2_3pts = 0 if discussion2W2_3pts == 1
replace discussion2W2_3pts = 1 if discussion2W2_3pts == 2
replace discussion2W2_3pts = 2 if discussion2W2_3pts == 3

egen discussionW2_3pts = rowmax(discussion1W2_3pts discussion2W2_3pts)

* Political discussion (wave 4)

gen discussion1W4 = 4 - w4_q79
gen discussion2W4 = 4 - w4_q83

replace discussion1W4 = 0 if sizeW4 == 0
replace discussion2W4 = 0 if sizeW4 == 0 | sizeW4 < 2

gen discussionW4 = discussion1W4 + discussion2W4

gen discussion1W4_3pts = discussion1W4
replace discussion1W4_3pts = 0 if discussion1W4_3pts == 1
replace discussion1W4_3pts = 1 if discussion1W4_3pts == 2
replace discussion1W4_3pts = 2 if discussion1W4_3pts == 3

gen discussion2W4_3pts = discussion2W4
replace discussion2W4_3pts = 0 if discussion2W4_3pts == 1
replace discussion2W4_3pts = 1 if discussion2W4_3pts == 2
replace discussion2W4_3pts = 2 if discussion2W4_3pts == 3

egen discussionW4_3pts = rowmax(discussion1W4_3pts discussion2W4_3pts)

* Political disagreement (wave 2)

gen disagreement1W2 = w2_q48
gen disagreement2W2 = w2_q52

recode disagreement1W2 disagreement2W2 (88=.)

replace disagreement1W2 = 5 - disagreement1W2
replace disagreement2W2 = 5 - disagreement2W2

replace disagreement1W2 = 0 if sizeW2 == 0
replace disagreement2W2 = 0 if sizeW2 == 0 | sizeW2 < 2

gen disagreementW2 = disagreement1W2 + disagreement2W2

gen disagreement1W2_3pts = disagreement1W2
gen disagreement2W2_3pts = disagreement2W2

replace disagreement1W2_3pts = 0 if disagreement1W2 == 1
replace disagreement1W2_3pts = 1 if disagreement1W2 == 2
replace disagreement1W2_3pts = 2 if disagreement1W2 == 3
replace disagreement1W2_3pts = 2 if disagreement1W2 == 4

replace disagreement2W2_3pts = 0 if disagreement2W2 == 1
replace disagreement2W2_3pts = 1 if disagreement2W2 == 2
replace disagreement2W2_3pts = 2 if disagreement2W2 == 3
replace disagreement2W2_3pts = 2 if disagreement2W2 == 4

egen disagreementW2_3pts = rowmax(disagreement1W2_3pts disagreement2W2_3pts)

* Political disagreement (wave 4)

gen disagreement1W4 = w4_q81
gen disagreement2W4 = w4_q85

recode disagreement1W4 disagreement2W4 (88=.)

replace disagreement1W4 = 5 - disagreement1W4
replace disagreement2W4 = 5 - disagreement2W4

replace disagreement1W4 = 0 if sizeW4 == 0
replace disagreement2W4 = 0 if sizeW4 == 0 | sizeW4 < 2

gen disagreementW4 = disagreement1W4 + disagreement2W4

gen disagreement1W4_3pts = disagreement1W4
gen disagreement2W4_3pts = disagreement2W4

replace disagreement1W4_3pts = 0 if disagreement1W4 == 1
replace disagreement1W4_3pts = 1 if disagreement1W4 == 2
replace disagreement1W4_3pts = 2 if disagreement1W4 == 3
replace disagreement1W4_3pts = 2 if disagreement1W4 == 4

replace disagreement2W4_3pts = 0 if disagreement2W4 == 1
replace disagreement2W4_3pts = 1 if disagreement2W4 == 2
replace disagreement2W4_3pts = 2 if disagreement2W4 == 3
replace disagreement2W4_3pts = 2 if disagreement2W4 == 4

egen disagreementW4_3pts = rowmax(disagreement1W4_3pts disagreement2W4_3pts)

* Coalition preference (wave 2)

gen coalpref1W2 = w2_q37x1
gen coalpref2W2 = w2_q37x2
gen coalpref3W2 = w2_q37x3
gen coalpref4W2 = w2_q38x1
gen coalpref5W2 = w2_q38x2
gen coalpref6W2 = w2_q38x3
gen coalpref7W2 = w2_q38x4

recode coalpref1W2 coalpref2W2 coalpref3W2 coalpref4W2 coalpref5W2 coalpref6W2 coalpref7W2 (88=.)
recode coalpref1W2 coalpref2W2 coalpref3W2 coalpref4W2 coalpref5W2 coalpref6W2 coalpref7W2 (99=.)

gen complete_coalprefW2 = 1 if coalpref1W2!=. & coalpref2W2!=. & coalpref3W2!=. & coalpref4W2!=. & coalpref5W2!=. & coalpref6W2!=. & coalpref7W2!=.

gen coalprefW2 = 3 if coalpref2W2 >= coalpref1W2 & coalpref2W2 >= coalpref3W2 & coalpref2W2 >= coalpref4W2 & coalpref2W2 >= coalpref5W2 & coalpref2W2 >= coalpref6W2 & coalpref2W2 >= coalpref7W2 & complete_coalprefW2 == 1
replace coalprefW2 = 2 if coalpref1W2 == coalpref2W2 & coalpref1W2 == coalpref3W2 & coalpref1W2 == coalpref4W2 & coalpref1W2 == coalpref5W2 & coalpref1W2 == coalpref6W2 & coalpref1W2 == coalpref7W2 & complete_coalprefW2 == 1
replace coalprefW2 = 1 if coalprefW2!=2 & coalprefW2!=3 & complete_coalprefW2 == 1

replace coalprefW2 = . if coalprefW2 == 3 & coalpref2W2 == coalpref1W2 & complete_coalprefW2 == 1
replace coalprefW2 = . if coalprefW2 == 3 & coalpref2W2 == coalpref3W2 & complete_coalprefW2 == 1
replace coalprefW2 = . if coalprefW2 == 3 & coalpref2W2 == coalpref4W2 & complete_coalprefW2 == 1
replace coalprefW2 = . if coalprefW2 == 3 & coalpref2W2 == coalpref5W2 & complete_coalprefW2 == 1
replace coalprefW2 = . if coalprefW2 == 3 & coalpref2W2 == coalpref6W2 & complete_coalprefW2 == 1
replace coalprefW2 = . if coalprefW2 == 3 & coalpref2W2 == coalpref7W2 & complete_coalprefW2 == 1

label define coalpref 1 "Loser coalition" 2 "No preference" 3 "Winner coalition"
label values coalprefW2 coalpref

* Coalition preference strength (wave 2)

gen coalstrength1W2 = coalpref1W2 if coalpref1W2 >= coalpref2W2 & coalpref1W2 >= coalpref3W2 & coalpref1W2 >= coalpref4W2 & coalpref1W2 >= coalpref5W2 & coalpref1W2 >= coalpref6W2 & coalpref1W2 >= coalpref7W2 & complete_coalprefW2 == 1
gen coalstrength2W2 = coalpref2W2 if coalpref2W2 >= coalpref1W2 & coalpref2W2 >= coalpref3W2 & coalpref2W2 >= coalpref4W2 & coalpref2W2 >= coalpref5W2 & coalpref2W2 >= coalpref6W2 & coalpref2W2 >= coalpref7W2 & complete_coalprefW2 == 1
gen coalstrength3W2 = coalpref3W2 if coalpref3W2 >= coalpref1W2 & coalpref3W2 >= coalpref2W2 & coalpref3W2 >= coalpref4W2 & coalpref3W2 >= coalpref5W2 & coalpref3W2 >= coalpref6W2 & coalpref3W2 >= coalpref7W2 & complete_coalprefW2 == 1
gen coalstrength4W2 = coalpref4W2 if coalpref4W2 >= coalpref1W2 & coalpref4W2 >= coalpref2W2 & coalpref4W2 >= coalpref3W2 & coalpref4W2 >= coalpref5W2 & coalpref4W2 >= coalpref6W2 & coalpref4W2 >= coalpref7W2 & complete_coalprefW2 == 1
gen coalstrength5W2 = coalpref5W2 if coalpref5W2 >= coalpref1W2 & coalpref5W2 >= coalpref2W2 & coalpref5W2 >= coalpref3W2 & coalpref5W2 >= coalpref4W2 & coalpref5W2 >= coalpref6W2 & coalpref5W2 >= coalpref7W2 & complete_coalprefW2 == 1
gen coalstrength6W2 = coalpref6W2 if coalpref6W2 >= coalpref1W2 & coalpref6W2 >= coalpref2W2 & coalpref6W2 >= coalpref3W2 & coalpref6W2 >= coalpref4W2 & coalpref6W2 >= coalpref5W2 & coalpref6W2 >= coalpref7W2 & complete_coalprefW2 == 1
gen coalstrength7W2 = coalpref7W2 if coalpref7W2 >= coalpref1W2 & coalpref7W2 >= coalpref2W2 & coalpref7W2 >= coalpref3W2 & coalpref7W2 >= coalpref4W2 & coalpref7W2 >= coalpref5W2 & coalpref7W2 >= coalpref6W2 & complete_coalprefW2 == 1

gen coalstrengthW2 = max(coalstrength1W2,coalstrength2W2,coalstrength3W2,coalstrength4W2,coalstrength5W2,coalstrength6W2,coalstrength7W2)

replace coalstrengthW2 = 300 if coalstrengthW2 == 7 | coalstrengthW2 == 8 | coalstrengthW2 == 9 | coalstrengthW2 == 10
replace coalstrengthW2 = 200 if coalstrengthW2 == 4 | coalstrengthW2 == 5 | coalstrengthW2 == 6
replace coalstrengthW2 = 100 if coalstrengthW2 == 0 | coalstrengthW2 == 1 | coalstrengthW2 == 2 | coalstrengthW2 == 3

replace coalstrengthW2 = 3 if coalstrengthW2 == 300
replace coalstrengthW2 = 2 if coalstrengthW2 == 200
replace coalstrengthW2 = 1 if coalstrengthW2 == 100

label define coalstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values coalstrengthW2 coalstrength

* Coalition preference scale (wave 2)

gen coalscaleW2 = .

replace coalscaleW2 = 1 if coalprefW2 == 1 & coalstrengthW2 == 3
replace coalscaleW2 = 2 if coalprefW2 == 1 & coalstrengthW2 == 2
replace coalscaleW2 = 3 if coalprefW2 == 1 & coalstrengthW2 == 1
replace coalscaleW2 = 4 if coalprefW2 == 2
replace coalscaleW2 = 5 if coalprefW2 == 3 & coalstrengthW2 == 1
replace coalscaleW2 = 6 if coalprefW2 == 3 & coalstrengthW2 == 2
replace coalscaleW2 = 7 if coalprefW2 == 3 & coalstrengthW2 == 3

label define coalscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values coalscaleW2 coalscale

* Coalition preference (wave 4)

gen coalpref1W4 = w4_q41x1
gen coalpref2W4 = w4_q41x2
gen coalpref3W4 = w4_q41x3

recode coalpref1W4 coalpref2W4 coalpref3W4 (88=.)
recode coalpref1W4 coalpref2W4 coalpref3W4 (99=.)

gen complete_coalprefW4 = 1 if coalpref1W4!=. & coalpref2W4!=. & coalpref3W4!=.

gen coalprefW4 = 3 if coalpref2W4 >= coalpref1W4 & coalpref2W4 >= coalpref3W4 & complete_coalprefW4 == 1
replace coalprefW4 = 2 if coalpref1W4 == coalpref2W4 & coalpref1W4 == coalpref3W4 & complete_coalprefW4 == 1
replace coalprefW4 = 1 if coalprefW4!=2 & coalprefW4!=3 & complete_coalprefW4 == 1

replace coalprefW4 = . if coalprefW4 == 3 & coalpref2W4 == coalpref1W4 & complete_coalprefW4 == 1
replace coalprefW4 = . if coalprefW4 == 3 & coalpref2W4 == coalpref3W4 & complete_coalprefW4 == 1

label values coalprefW4 coalpref

* Coalition preference strength (wave 4)

gen coalstrength1W4 = coalpref1W4 if coalpref1W4 >= coalpref2W4 & coalpref1W4 >= coalpref3W4 & complete_coalprefW4 == 1
gen coalstrength2W4 = coalpref2W4 if coalpref2W4 >= coalpref1W4 & coalpref2W4 >= coalpref3W4 & complete_coalprefW4 == 1
gen coalstrength3W4 = coalpref3W4 if coalpref3W4 >= coalpref1W4 & coalpref3W4 >= coalpref2W4 & complete_coalprefW4 == 1

gen coalstrengthW4 = max(coalstrength1W4,coalstrength2W4,coalstrength3W4)

replace coalstrengthW4 = 300 if coalstrengthW4 == 7 | coalstrengthW4 == 8 | coalstrengthW4 == 9 | coalstrengthW4 == 10
replace coalstrengthW4 = 200 if coalstrengthW4 == 4 | coalstrengthW4 == 5 | coalstrengthW4 == 6
replace coalstrengthW4 = 100 if coalstrengthW4 == 0 | coalstrengthW4 == 1 | coalstrengthW4 == 2 | coalstrengthW4 == 3

replace coalstrengthW4 = 3 if coalstrengthW4 == 300
replace coalstrengthW4 = 2 if coalstrengthW4 == 200
replace coalstrengthW4 = 1 if coalstrengthW4 == 100

label values coalstrengthW4 coalstrength

* Coalition preference scale (wave 4)

gen coalscaleW4 = .

replace coalscaleW4 = 1 if coalprefW4 == 1 & coalstrengthW4 == 3
replace coalscaleW4 = 2 if coalprefW4 == 1 & coalstrengthW4 == 2
replace coalscaleW4 = 3 if coalprefW4 == 1 & coalstrengthW4 == 1
replace coalscaleW4 = 4 if coalprefW4 == 2
replace coalscaleW4 = 5 if coalprefW4 == 3 & coalstrengthW4 == 1
replace coalscaleW4 = 6 if coalprefW4 == 3 & coalstrengthW4 == 2
replace coalscaleW4 = 7 if coalprefW4 == 3 & coalstrengthW4 == 3

label values coalscaleW4 coalition_scale_label


*******************************************
** NATIONAL-LEVEL EXPECTATIONS IN WAVE 2 ** 
*******************************************

* Generate variable identifying incomplete answers

gen winCoalition1W2 = w2_q35x1
gen winCoalition2W2 = w2_q35x2
gen winCoalition3W2 = w2_q35x3
gen winCoalition4W2 = w2_q36x1
gen winCoalition5W2 = w2_q36x2
gen winCoalition6W2 = w2_q36x3
gen winCoalition7W2 = w2_q36x4

recode winCoalition1W2 winCoalition2W2 winCoalition3W2 winCoalition4W2 winCoalition5W2 winCoalition6W2 winCoalition7W2 (88=.)
recode winCoalition1W2 winCoalition2W2 winCoalition3W2 winCoalition4W2 winCoalition5W2 winCoalition6W2 winCoalition7W2 (99=.)

gen completeW2_whole = 1 if winCoalition1W2!=. & winCoalition2W2!=. & winCoalition3W2!=. & winCoalition4W2!=. & winCoalition5W2!=. & winCoalition6W2!=. & winCoalition7W2!=.

* Generate value of 1 if the expectation for one coalition is higher or equal to the expectations for at least one other coalition

gen coalition1W2 = 1 if winCoalition1W2 >= winCoalition2W2 & winCoalition1W2 >= winCoalition3W2 & winCoalition1W2 >= winCoalition4W2 & winCoalition1W2 >= winCoalition5W2 & winCoalition1W2 >= winCoalition6W2 & winCoalition1W2 >= winCoalition7W2 & completeW2_whole == 1

gen coalition2W2 = 1 if winCoalition2W2 >= winCoalition1W2 & winCoalition2W2 >= winCoalition3W2 & winCoalition2W2 >= winCoalition4W2 & winCoalition2W2 >= winCoalition5W2 & winCoalition2W2 >= winCoalition6W2 & winCoalition2W2 >= winCoalition7W2 & completeW2_whole == 1

gen coalition3W2 = 1 if winCoalition3W2 >= winCoalition1W2 & winCoalition3W2 >= winCoalition2W2 & winCoalition3W2 >= winCoalition4W2 & winCoalition3W2 >= winCoalition5W2 & winCoalition3W2 >= winCoalition6W2 & winCoalition3W2 >= winCoalition7W2 & completeW2_whole == 1

gen coalition4W2 = 1 if winCoalition4W2 >= winCoalition1W2 & winCoalition4W2 >= winCoalition2W2 & winCoalition4W2 >= winCoalition3W2 & winCoalition4W2 >= winCoalition5W2 & winCoalition4W2 >= winCoalition6W2 & winCoalition4W2 >= winCoalition7W2 & completeW2_whole == 1

gen coalition5W2 = 1 if winCoalition5W2 >= winCoalition1W2 & winCoalition5W2 >= winCoalition2W2 & winCoalition5W2 >= winCoalition3W2 & winCoalition5W2 >= winCoalition4W2 & winCoalition5W2 >= winCoalition6W2 & winCoalition5W2 >= winCoalition7W2 & completeW2_whole == 1

gen coalition6W2 = 1 if winCoalition6W2 >= winCoalition1W2 & winCoalition6W2 >= winCoalition2W2 & winCoalition6W2 >= winCoalition3W2 & winCoalition6W2 >= winCoalition4W2 & winCoalition6W2 >= winCoalition5W2 & winCoalition6W2 >= winCoalition7W2 & completeW2_whole == 1

gen coalition7W2 = 1 if winCoalition7W2 >= winCoalition1W2 & winCoalition7W2 >= winCoalition2W2 & winCoalition7W2 >= winCoalition3W2 & winCoalition7W2 >= winCoalition4W2 & winCoalition7W2 >= winCoalition5W2 & winCoalition7W2 >= winCoalition6W2 & completeW2_whole == 1

gen coalitionOTHW2 = 1 if winCoalition1W2 == 0 & winCoalition2W2 == 0 & winCoalition3W2 == 0 & winCoalition4W2 == 0 & winCoalition5W2 == 0 & winCoalition6W2 == 0 & winCoalition7W2 == 0 & completeW2_whole == 1

replace coalition1W2 = . if coalitionOTHW2 == 1 & completeW2_whole == 1
replace coalition2W2 = . if coalitionOTHW2 == 1 & completeW2_whole == 1
replace coalition3W2 = . if coalitionOTHW2 == 1 & completeW2_whole == 1
replace coalition4W2 = . if coalitionOTHW2 == 1 & completeW2_whole == 1
replace coalition5W2 = . if coalitionOTHW2 == 1 & completeW2_whole == 1
replace coalition6W2 = . if coalitionOTHW2 == 1 & completeW2_whole == 1
replace coalition7W2 = . if coalitionOTHW2 == 1 & completeW2_whole == 1

* Generate variable for ambiguous forecasts

gen ambiguousW2_whole = .

replace ambiguousW2_whole = 99 if coalition1W2 == coalition2W2 & coalition1W2!=. | coalition1W2 == coalition3W2 & coalition1W2!=. | coalition1W2 == coalition4W2 & coalition1W2!=. | coalition1W2 == coalition5W2 & coalition1W2!=. | coalition1W2 == coalition6W2 & coalition1W2!=. | coalition1W2 == coalition7W2 & coalition1W2!=.

replace ambiguousW2_whole = 99 if coalition2W2 == coalition1W2 & coalition2W2!=. | coalition2W2 == coalition3W2 & coalition2W2!=. | coalition2W2 == coalition4W2 & coalition2W2!=. | coalition2W2 == coalition5W2 & coalition2W2!=. | coalition2W2 == coalition6W2 & coalition2W2!=. | coalition2W2 == coalition7W2 & coalition2W2!=.

replace ambiguousW2_whole = 99 if coalition3W2 == coalition1W2 & coalition3W2!=. | coalition3W2 == coalition2W2 & coalition3W2!=. | coalition3W2 == coalition4W2 & coalition3W2!=. | coalition3W2 == coalition5W2 & coalition3W2!=. | coalition3W2 == coalition6W2 & coalition3W2!=. | coalition3W2 == coalition7W2 & coalition3W2!=.

replace ambiguousW2_whole = 99 if coalition4W2 == coalition1W2 & coalition4W2!=. | coalition4W2 == coalition2W2 & coalition4W2!=. | coalition4W2 == coalition3W2 & coalition4W2!=. | coalition4W2 == coalition5W2 & coalition4W2!=. | coalition4W2 == coalition6W2 & coalition4W2!=. | coalition4W2 == coalition7W2 & coalition4W2!=.

replace ambiguousW2_whole = 99 if coalition5W2 == coalition1W2 & coalition5W2!=. | coalition5W2 == coalition2W2 & coalition5W2!=. | coalition5W2 == coalition3W2 & coalition5W2!=. | coalition5W2 == coalition4W2 & coalition5W2!=. | coalition5W2 == coalition6W2 & coalition5W2!=. | coalition5W2 == coalition7W2 & coalition5W2!=.

replace ambiguousW2_whole = 99 if coalition6W2 == coalition1W2 & coalition6W2!=. | coalition6W2 == coalition2W2 & coalition6W2!=. | coalition6W2 == coalition3W2 & coalition6W2!=. | coalition6W2 == coalition4W2 & coalition6W2!=. | coalition6W2 == coalition5W2 & coalition6W2!=. | coalition6W2 == coalition7W2 & coalition6W2!=.

replace ambiguousW2_whole = 99 if coalition7W2 == coalition1W2 & coalition7W2!=. | coalition7W2 == coalition2W2 & coalition7W2!=. | coalition7W2 == coalition3W2 & coalition7W2!=. | coalition7W2 == coalition4W2 & coalition7W2!=. | coalition7W2 == coalition5W2 & coalition7W2!=. | coalition7W2 == coalition6W2 & coalition7W2!=.

* Generate value of 0 for lower expectations

replace coalition1W2 = 0 if coalition1W2!=1 & coalition1W2!=. & completeW2_whole == 1
replace coalition2W2 = 0 if coalition2W2!=1 & coalition2W2!=. & completeW2_whole == 1
replace coalition3W2 = 0 if coalition3W2!=1 & coalition3W2!=. & completeW2_whole == 1
replace coalition4W2 = 0 if coalition4W2!=1 & coalition4W2!=. & completeW2_whole == 1
replace coalition5W2 = 0 if coalition5W2!=1 & coalition5W2!=. & completeW2_whole == 1
replace coalition6W2 = 0 if coalition6W2!=1 & coalition6W2!=. & completeW2_whole == 1
replace coalition7W2 = 0 if coalition7W2!=1 & coalition7W2!=. & completeW2_whole == 1
replace coalitionOTHW2 = 0 if coalitionOTHW2!=1 & coalitionOTHW2!=. & completeW2_whole == 1

* Forecasts (1 = SPO-OVP, 2 = OVP-FPO, 3 = SPO-FPO, 4 = SPO-OVP-Greens, 5 = OVP-Greens-NEOS, 6 = SPO-Greens-NEOS, 7 = SPO-OVP-NEOS)

gen forecastW2_whole = .

replace forecastW2_whole = 1 if coalition1W2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 2 if coalition2W2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 3 if coalition3W2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 4 if coalition4W2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 5 if coalition5W2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 6 if coalition6W2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 7 if coalition7W2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 88 if coalitionOTHW2 == 1 & completeW2_whole == 1
replace forecastW2_whole = 99 if ambiguousW2_whole == 1 & completeW2_whole == 1

* Identify correct and ambiguous forecasts

gen correctW2_whole = 0 if forecastW2_whole!=2 & forecastW2_whole!=99 & completeW2_whole == 1

replace correctW2_whole = 1 if forecastW2_whole == 2 & forecastW2_whole!=99 & forecastW2_whole!=. & completeW2_whole == 1

replace correctW2_whole = 0 if w2_q35x1 == 88 & w2_q35x2 == 88 & w2_q35x3 == 88 & w2_q36x1 == 88 & w2_q36x2 == 88 & w2_q36x3 == 88 & w2_q36x4 == 88

replace correctW2_whole = 99 if forecastW2_whole == 99 & coalition2W2 == 1 & completeW2_whole == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correctW2_whole_d = correctW2_whole

replace correctW2_whole_d = 0 if correctW2_whole_d == 99
replace correctW2_whole_d = 1 if correctW2_whole_d == 1


*******************************************
** NATIONAL-LEVEL EXPECTATIONS IN WAVE 4 ** 
*******************************************

gen winCoalition1W4 = w4_q40x1
gen winCoalition2W4 = w4_q40x2
gen winCoalition3W4 = w4_q40x3

recode winCoalition1W4 winCoalition2W4 winCoalition3W4 (88=.)
recode winCoalition1W4 winCoalition2W4 winCoalition3W4 (99=.)

gen completeW4_whole = 1 if winCoalition1W4!=. & winCoalition2W4!=. & winCoalition3W4!=.

* Generate value of 1 if the expectation for one coalition is higher or equal to the expectations for at least one other coalition

gen coalition1W4 = 1 if winCoalition1W4 >= winCoalition2W4 & winCoalition1W4 >= winCoalition3W4 & completeW4_whole == 1

gen coalition2W4 = 1 if winCoalition2W4 >= winCoalition1W4 & winCoalition2W4 >= winCoalition3W4 & completeW4_whole == 1

gen coalition3W4 = 1 if winCoalition3W4 >= winCoalition1W4 & winCoalition3W4 >= winCoalition2W4 & completeW4_whole == 1

gen coalitionOTHW4 = 1 if winCoalition1W4 == 0 & winCoalition2W4 == 0 & winCoalition3W4 == 0 & completeW4_whole == 1

replace coalition1W4 = . if coalitionOTHW4 == 1 & completeW4_whole == 1
replace coalition2W4 = . if coalitionOTHW4 == 1 & completeW4_whole == 1
replace coalition3W4 = . if coalitionOTHW4 == 1 & completeW4_whole == 1

* Generate variable for ambiguous forecasts

gen ambiguousW4_whole = .

replace ambiguousW4_whole = 99 if coalition1W4 == coalition2W4 & coalition1W4!=. | coalition1W4 == coalition3W4 & coalition1W4!=.

replace ambiguousW4_whole = 99 if coalition2W4 == coalition1W4 & coalition2W4!=. | coalition2W4 == coalition3W4 & coalition2W4!=.

replace ambiguousW4_whole = 99 if coalition3W4 == coalition1W4 & coalition3W4!=. | coalition3W4 == coalition2W4 & coalition3W4!=.

* Generate value of 0 for lower expectations

replace coalition1W4 = 0 if coalition1W4!=1 & coalition1W4!=. & completeW4_whole == 1
replace coalition2W4 = 0 if coalition2W4!=1 & coalition2W4!=. & completeW4_whole == 1
replace coalition3W4 = 0 if coalition3W4!=1 & coalition3W4!=. & completeW4_whole == 1
replace coalitionOTHW4 = 0 if coalitionOTHW4!=1 & coalitionOTHW4!=. & completeW4_whole == 1

* Forecasts (1 = SPO-OVP, 2 = OVP-FPO, 3 = SPO-FPO)

gen forecastW4_whole = .

replace forecastW4_whole = 1 if coalition1W4 == 1 & completeW4_whole == 1
replace forecastW4_whole = 2 if coalition2W4 == 1 & completeW4_whole == 1
replace forecastW4_whole = 3 if coalition3W4 == 1 & completeW4_whole == 1
replace forecastW4_whole = 88 if coalitionOTHW4 == 1 & completeW4_whole == 1
replace forecastW4_whole = 99 if ambiguousW4_whole == 1 & completeW4_whole == 1

* Identify correct and ambiguous forecasts

gen correctW4_whole = 0 if forecastW4_whole!=2 & forecastW4_whole!=99 & completeW4_whole == 1

replace correctW4_whole = 1 if forecastW4_whole == 2 & forecastW4_whole!=99 & forecastW4_whole!=. & completeW4_whole == 1

replace correctW4_whole = 0 if w4_q40x1 == 88 & w4_q40x2 == 88 & w4_q40x3 == 88

replace correctW4_whole = 99 if forecastW4_whole == 99 & coalition2W4 == 1 & completeW4_whole == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correctW4_whole_d = correctW4_whole

replace correctW4_whole_d = 0 if correctW4_whole_d == 99
replace correctW4_whole_d = 1 if correctW4_whole_d == 1


**********
** SAVE **
**********

save "at2017_autnes.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts

logistic correctW4_whole_d discussionW4 disagreementW4 sizeW4 coalscaleW4 interestW4 news age i.male education timeW4
estimates table, star(.05 .01 .001)

tab correctW4_whole_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correctW4_whole_d "Coalition formed (DV)"
label variable discussionW4 "Discussion"
label variable disagreementW4 "Disagreement"
label variable sizeW4 "Network size"
label variable coalscaleW4 "Coalition preference"
label variable pidscale_whole "National PID"
label variable interestW4 "Interest"
label variable news "News attentiveness"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable timeW4 "Time of interview"

estpost tabstat correctW4_whole_d discussionW4 disagreementW4 sizeW4 coalscaleW4 interestW4 news age male education timeW4 if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "at2017_autnes_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2017 Austrian legislative election (AUTNES), national level") replace

eststo clear

log close